<?php
namespace app\backstage\logic;
use think\Db;

class Report
{
    public function getList()
    {
        $field = 'id,channel_id,advert_account';

        if (request()->_role->alias == 'agent') {
            $params['channel_id'] = request()->_user->id;
        }

        $condition[] = ['channel_id', 'eq', 'gt:0','channel_id'];
        $condition[] = ['advert_account', 'like', 'require','advert_account'];

        $where   = buildQueryCondition($condition, $params);

        if( isset($params['field']) ){
            $orderBy = [ $params['field'] => $params['order'] ];
        }else{
            $orderBy = ['id' => 'DESC'];
        }

        $AdvertUser = \think\facade\App::model('AdvertUser');
        $list = $AdvertUser->getList($where, $field, $params['page'], $params['limit'], $orderBy);

        if($list){

            $agent_id_gh = array_unique(array_column($list,'channel_id'));
            $agents = Db::table('cms_manager_user')->where('id','in',$agent_id_gh)->column('id,account');

            foreach($list as $k=>$v)
            {
                $list[$k]['agent_name'] = isset($agents[$v['channel_id']]) ? $agents[$v['channel_id']] : '';
            }
        }

        $result['count']    = $AdvertUser->getTotalNumber($where);
        $result['data']     = $list;
        return $result;
    }
}